import config from '@/common/config'
import {
  Base64
} from '@/vendor/base64.js';

module.exports = {
	data() {
		return {
			fileList: [],
			isUpload: false
		}
	},
	props: {

	},
	mounted() {},
	computed: {},
	methods: {
		// 删除图片
		deletePic(event) {
			this.fileList.splice(event.index, 1)
			
		},
		// 新增图片
		async afterRead(event,listss, name) {
			// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file)
			let fileListLen = this.fileList.length
			lists.map((item) => {
				this.fileList.push({
					...item,
					status: 'uploading',
					message: '上传中'
				})
			})
			this.isUpload = true
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url)
				let item = this.fileList[fileListLen]
				this.fileList.splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					url: result.link,
					name: result.name,
					originalName: result.originalName
				}))
				fileListLen++
			}
			this.isUpload = false
		},
		uploadFilePromise(url) {
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					url: `${config.baseUrl}/paas-resource/oss/endpoint/put-file`,
					filePath: url,
					name: 'file',
					formData: {},
					header: {
						'Authorization': `Basic ${Base64.encode(`${this.clientId}:${this.clientSecret}`)}`,
						[this.tokenHeader]: `bearer ${this.accessToken}`,
					},
					success: (res) => {
						const data = JSON.parse(res.data)
						// resolve(data.data.link)
						resolve(data.data)
					}
				});
			})
		},
	}
}
